package com.longcai.service.impl.property;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.advice.RollBackExecption;
import com.longcai.bean.ApiResult;
import com.longcai.constants.BaseConstants;
import com.longcai.constants.PromptConstants;
import com.longcai.entity.customer.ClyjUserProperty;
import com.longcai.entity.order.ClyjGoodsOrderDetails;
import com.longcai.entity.property.ClyjUserPropertyRecord;
import com.longcai.mapper.property.ClyjUserPropertyMapper;
import com.longcai.mapper.property.ClyjUserPropertyRecordMapper;
import com.longcai.service.property.ClyjUserPropertyService;
import com.longcai.util.StringUtils;
import com.longcai.vo.ClyjGoodsOrderDetailsVo;

/**
 * 资金变动（红包，零钱，积分），资金变动记录
 * 查询用户资产（红包，零钱，积分）
 *
 * @author Administrator
 */
@Service
public class ClyjUserPropertyServiceImpl extends ServiceImpl<ClyjUserPropertyMapper, ClyjUserProperty> implements ClyjUserPropertyService {

    private static Logger logger = LoggerFactory.getLogger(ClyjUserPropertyServiceImpl.class);

    @Autowired
    private ClyjUserPropertyMapper clyjUserPropertyMapper;

    @Autowired
    private ClyjUserPropertyRecordMapper clyjUserPropertyRecordMapper;

    /****
     * 资金变动（红包，零钱，积分）
     * @param userId    :用户id
     * @param value        :值（零钱，红包，积分,余额）
     * @param expenses    :开支（0收入 1支出）
     * @param source    ：来源途径
     * @param assetType    ：资产类型(0 零钱 1红包 2积分3余额)
     * @param tradeNo    ： 交易流水号(对于支出类型，涉及交易，有流水号，可存)
     * @param freeze    : 冻结字段（0.冻结， 1.不冻结）
     * @return
     * @throws RollBackExecption 
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public ApiResult<?> changeFinance(String userId, BigDecimal value, String expenses, String source, String assetType, String tradeNo, String freeze, String cardNo) throws RollBackExecption {
        ApiResult<?> r = checkParam(userId, value, expenses, source, assetType, tradeNo, freeze);
        if (!r.getCode().equals(BaseConstants.SUCCESS)) {
            return r;
        }
        /**
         * 开支 （0收入 1支出）
         * assetType：资产类型(0 零钱 1红包 2积分3余额)
         * **/
        ClyjUserProperty clyjUserProperty = queryUserProperty(userId);
        if (clyjUserProperty == null) {
            /**对象为空初始化**/
            clyjUserProperty = initializeClyjUserProperty(clyjUserProperty, userId);
            clyjUserProperty.setCreateTime(LocalDateTime.now());
            ClyjUserProperty clyjUserPropertys = incomeClyjUserProperty(clyjUserProperty, assetType, value);
            Integer i = clyjUserPropertyMapper.insert(clyjUserPropertys);
            if (i < 1) {
                logger.error("资金变动---对象为空初始化---+" + "userId:" + userId + "Time:" + LocalDateTime.now() + PromptConstants.CHANGE_FINANCE_FAIL);
                return ApiResult.fail(PromptConstants.CHANGE_FINANCE_FAIL);
            }
            return ApiResult.success();
        }
        try {
        if (cardNo == null || cardNo.equals("")) {
            ClyjUserPropertyRecord clyjUserPropertyRecord = save(userId, value, expenses, source, assetType, tradeNo, null);
            if (expenses.equals(BaseConstants.INCOME_CODE)) {
                /**freeze    : 冻结字段（0.冻结， 1.不冻结）**/
                if (freeze.equals(BaseConstants.FREEZE)) {
                    clyjUserProperty = incomeFreeze(clyjUserProperty, assetType, value, userId);
                    //加冻结标示
                    clyjUserPropertyRecord.setState(BaseConstants.DISABLED_STATE);
                } else {
                    clyjUserProperty = income(clyjUserProperty, assetType, value, userId);
                }
            } else if (expenses.equals(BaseConstants.PAY_CODE)) {
                /**freeze    : 冻结字段（0.冻结， 1.不冻结）**/
                ApiResult<?> rep = checkPay(clyjUserProperty, value, assetType, tradeNo);
                if (!rep.getCode().equals(BaseConstants.SUCCESS)) {
                    return rep;
                }
                clyjUserProperty = pay(clyjUserProperty, assetType, value, userId);
                //加冻结标示
                clyjUserPropertyRecord.setState(BaseConstants.DISABLED_STATE);
            }
            Integer i = clyjUserPropertyMapper.updateById(clyjUserProperty);
            Integer j = clyjUserPropertyRecordMapper.insert(clyjUserPropertyRecord);
            if(i==0||j==0){
                logger.error("资金变动--cardNo == null || cardNo.equals()---失败---+" + "clyjUserPropertyMapper.updateById(clyjUserProperty)-----------+i:" + i +"---------------------"
                		+ "clyjUserPropertyRecordMapper.insert(clyjUserPropertyRecord)-----------+j:" + j +"---------------------");
            	int a=1/0;
            }
        } else {
            ClyjUserPropertyRecord clyjUserPropertyRecord = save(userId, value, expenses, source, assetType, tradeNo, cardNo);
            if (expenses.equals(BaseConstants.INCOME_CODE)) {
                /**freeze    : 冻结字段（0.冻结， 1.不冻结）**/
                if (freeze.equals(BaseConstants.FREEZE)) {
                    clyjUserProperty = incomeFreeze(clyjUserProperty, assetType, value, userId);
                    //加冻结标示
                    clyjUserPropertyRecord.setState(BaseConstants.DISABLED_STATE);
                } else {
                    clyjUserProperty = income(clyjUserProperty, assetType, value, userId);
                }
            } else if (expenses.equals(BaseConstants.PAY_CODE)) {
                /**freeze    : 冻结字段（0.冻结， 1.不冻结）**/
                ApiResult<?> rep = checkPay(clyjUserProperty, value, assetType, tradeNo);
                if (!rep.getCode().equals(BaseConstants.SUCCESS)) {
                    return rep;
                }
                clyjUserProperty = pay(clyjUserProperty, assetType, value, userId);
                //加冻结标示
                clyjUserPropertyRecord.setState(BaseConstants.DISABLED_STATE);
            }
            Integer i = clyjUserPropertyMapper.updateById(clyjUserProperty);
            Integer j = clyjUserPropertyRecordMapper.insert(clyjUserPropertyRecord);
            if(i==0||j==0){
                logger.error("资金变动----失败---+" + "clyjUserPropertyMapper.updateById(clyjUserProperty)-----------+i:" + i +"---------------------"
                		+ "clyjUserPropertyRecordMapper.insert(clyjUserPropertyRecord)-----------+j:" + j +"---------------------");
            	int a=1/0;
            }
        } 
        }catch (Exception e) {
			throw new RollBackExecption("资产变动失败");
		}
        return ApiResult.success();
    }


    /**
     * 收入冻结
     **/
    private ClyjUserProperty incomeFreeze(ClyjUserProperty clyjUserProperty, String assetType, BigDecimal value, String userId) {
        /**assetType	：资产类型(0 零钱 1红包 2积分3余额)**/
        if (assetType.equals(BaseConstants.SHOESTRING_MONEY)) {
            clyjUserProperty.setFreezeShoestringMoney(clyjUserProperty.getFreezeShoestringMoney().add(value));
        } else if (assetType.equals(BaseConstants.RED_PACKET)) {
            clyjUserProperty.setFreezeRedPacket(clyjUserProperty.getFreezePayRedPacket().add(value));
        } else if (assetType.equals(BaseConstants.INTEGRAL)) {
            clyjUserProperty.setFreezeIntegral(clyjUserProperty.getFreezeIntegral().add(value));
        } else if (assetType.equals(BaseConstants.ACCOUNT)) {
            clyjUserProperty.setFreezeAccount(clyjUserProperty.getFreezeAccount().add(value));
        }
        clyjUserProperty.setUpdateTime(LocalDateTime.now());
        return clyjUserProperty;
    }

    /****
     * 查询用户资产
     * @return：用户资产表
     */
    @Override
    public ClyjUserProperty queryUserProperty(String userId) {
        QueryWrapper<ClyjUserProperty> queryWrapper = new QueryWrapper<ClyjUserProperty>();
        queryWrapper.eq("user_id", userId);
        ClyjUserProperty clyjUserProperty = clyjUserPropertyMapper.selectOne(queryWrapper);
        return clyjUserProperty;
    }

    /**
     * 资金变动请求参数校验
     **/
    public ApiResult<?> checkParam(String userId, BigDecimal value, String expenses, String source,
                                   String assetType, String tradeNo, String freeze) {
    	int i=value.compareTo(BigDecimal.ZERO); 
    	if(i==-1){ 
    		logger.error("资金变动参数非法:"+"value"+value);
    		return ApiResult.fail(PromptConstants.PARAMETER_ILLEGAL);
    	   } 
        if (StringUtils.isEmpty(expenses) || StringUtils.isEmpty(source) || StringUtils.isEmpty(assetType) || StringUtils.isEmpty(freeze)) {
            logger.error("资金变动参数非法:"
                    + "expenses:开支（0收入 1支出)" + expenses + "--------"
                    + "source：来源途径" + source + "--------"
                    + "assetType：资产类型(0 零钱 1红包 2积分" + assetType + "--------"
                    + "freeze：是否冻结(0 冻结 1不冻结" + freeze + "--------"
            );
            return ApiResult.fail(PromptConstants.PARAMETER_ILLEGAL);
        }
        return ApiResult.success();
    }

    /**
     * 对象为空初始化
     **/
    public ClyjUserProperty initializeClyjUserProperty(ClyjUserProperty clyjUserProperty, String userId) {
        clyjUserProperty = new ClyjUserProperty();
        clyjUserProperty.setIntegral(new BigDecimal("0.000"));
        clyjUserProperty.setFreezeIntegral(new BigDecimal("0.000"));
        clyjUserProperty.setFreezePayIntegral(new BigDecimal("0.000"));

        clyjUserProperty.setRedPacket(new BigDecimal("0.000"));
        clyjUserProperty.setFreezeRedPacket(new BigDecimal("0.000"));
        clyjUserProperty.setFreezePayRedPacket(new BigDecimal("0.000"));

        clyjUserProperty.setShoestringMoney(new BigDecimal("0.000"));
        clyjUserProperty.setFreezeShoestringMoney(new BigDecimal("0.000"));
        clyjUserProperty.setFreezePayShoestringMoney(new BigDecimal("0.000"));

        clyjUserProperty.setAccount(new BigDecimal("0.000"));
        clyjUserProperty.setFreezeAccount(new BigDecimal("0.000"));
        clyjUserProperty.setFreezePayAccount(new BigDecimal("0.000"));

        clyjUserProperty.setUserId(userId);
        clyjUserProperty.setUpdateTime(LocalDateTime.now());
        return clyjUserProperty;
    }

    /**
     * 开支类型:收入
     **/
    public ClyjUserProperty income(ClyjUserProperty clyjUserProperty, String assetType, BigDecimal value, String userId) {
        //assetType：资产类型(0 零钱 1红包 2积分)
        clyjUserProperty = incomeClyjUserProperty(clyjUserProperty, assetType, value);
        clyjUserProperty.setUpdateTime(LocalDateTime.now());
        return clyjUserProperty;
    }

    /**
     * 收入
     **/
    public ClyjUserProperty incomeClyjUserProperty(ClyjUserProperty clyjUserProperty, String assetType, BigDecimal value) {
        /**assetType	：资产类型(0 零钱 1红包 2积分3余额)**/
        if (assetType.equals(BaseConstants.SHOESTRING_MONEY)) {
            clyjUserProperty.setShoestringMoney(clyjUserProperty.getShoestringMoney().add(value));
        } else if (assetType.equals(BaseConstants.RED_PACKET)) {
            clyjUserProperty.setRedPacket(clyjUserProperty.getRedPacket().add(value));
        } else if (assetType.equals(BaseConstants.INTEGRAL)) {
            clyjUserProperty.setIntegral(clyjUserProperty.getIntegral().add(value));
        } else if (assetType.equals(BaseConstants.ACCOUNT)) {
            clyjUserProperty.setAccount(clyjUserProperty.getAccount().add(value));
        }
        return clyjUserProperty;
    }


    /**
     * 开支类型:支出
     **/
    public ClyjUserProperty pay(ClyjUserProperty clyjUserProperty, String assetType, BigDecimal value, String userId) {
        if (assetType.equals(BaseConstants.SHOESTRING_MONEY)) {
            clyjUserProperty.setFreezePayShoestringMoney(value);
        } else if (assetType.equals(BaseConstants.RED_PACKET)) {
            clyjUserProperty.setFreezePayRedPacket(value);
        } else if (assetType.equals(BaseConstants.INTEGRAL)) {
            clyjUserProperty.setFreezePayIntegral(value);
        } else if (assetType.equals(BaseConstants.ACCOUNT)) {
            clyjUserProperty.setFreezePayAccount(value);
        }
        clyjUserProperty.setUpdateTime(LocalDateTime.now());
        return clyjUserProperty;
    }

    /**
     * 资金变动(支出)参数校验
     **/
    public ApiResult<?> checkPay(ClyjUserProperty clyjUserProperty, BigDecimal value, String assetType, String tradeNo) {
        if (StringUtils.isEmpty(tradeNo)) {
            return ApiResult.fail(PromptConstants.TRADE_NO_ISNOTEMPTY);
        }
        /** assetType	：资产类型(0 零钱 1红包 2积分3余额)**/
        if (assetType.equals(BaseConstants.SHOESTRING_MONEY)) {
            int i = (clyjUserProperty.getShoestringMoney().subtract(clyjUserProperty.getFreezePayShoestringMoney())).compareTo(value);
            if (i == -1) {
                return ApiResult.fail(PromptConstants.SHOESTRING_MONEY_LACKING);
            }
        } else if (assetType.equals(BaseConstants.RED_PACKET)) {
            int i = (clyjUserProperty.getRedPacket().subtract(clyjUserProperty.getFreezePayRedPacket())).compareTo(value);
            if (i == -1) {
                return ApiResult.fail(PromptConstants.RED_PACKET_LACKING);
            }
        } else if (assetType.equals(BaseConstants.INTEGRAL)) {
            int i = (clyjUserProperty.getIntegral().subtract(clyjUserProperty.getFreezePayIntegral())).compareTo(value);
            if (i == -1) {
                return ApiResult.fail(PromptConstants.RED_PACKET_LACKING);
            }
        } else if (assetType.equals(BaseConstants.ACCOUNT)) {
            int i = (clyjUserProperty.getAccount().subtract(clyjUserProperty.getFreezeAccount())).compareTo(value);
            if (i == -1) {
                return ApiResult.fail(PromptConstants.ACCOUNT_LACKING);
            }
        }
        return ApiResult.success();
    }


    /****
     * 冻结
     */
    public ClyjUserProperty freeze(ClyjUserProperty clyjUserProperty, String assetType, BigDecimal value, String userId) {
        /**assetType	：资产类型(0 零钱 1红包 2积分3余额)**/
        if (assetType.equals(BaseConstants.SHOESTRING_MONEY)) {
            clyjUserProperty.setFreezeShoestringMoney(clyjUserProperty.getFreezeShoestringMoney().add(value));
        } else if (assetType.equals(BaseConstants.RED_PACKET)) {
            clyjUserProperty.setFreezeRedPacket(clyjUserProperty.getFreezeRedPacket().add(value));
        } else if (assetType.equals(BaseConstants.INTEGRAL)) {
            clyjUserProperty.setFreezeIntegral(clyjUserProperty.getFreezeIntegral().add(value));
        } else if (assetType.equals(BaseConstants.ACCOUNT)) {
            clyjUserProperty.setFreezeAccount(clyjUserProperty.getFreezeAccount().add(value));
        }
        clyjUserProperty.setUpdateTime(LocalDateTime.now());
        return clyjUserProperty;

    }

    /**
     * 存储用户资产收益记录
     **/
    public ClyjUserPropertyRecord save(String userId, BigDecimal value, String expenses, String source,
                                       String assetType, String tradeNo, String cardNo) {
        ClyjUserPropertyRecord clyjUserPropertyRecord = new ClyjUserPropertyRecord();
        clyjUserPropertyRecord.setId(UUID.randomUUID().toString().replace("-", ""));
        clyjUserPropertyRecord.setUserId(userId);
        clyjUserPropertyRecord.setValue(value);
        clyjUserPropertyRecord.setExpenses(expenses);
        clyjUserPropertyRecord.setSource(source);
        clyjUserPropertyRecord.setAssetType(assetType);
        clyjUserPropertyRecord.setCreateTime(LocalDateTime.now());
        clyjUserPropertyRecord.setTradeNo(tradeNo);
        clyjUserPropertyRecord.setCardNo(cardNo);
        return clyjUserPropertyRecord;
    }


    @Override
    public ApiResult<?> thaw(ClyjGoodsOrderDetailsVo clyjGoodsOrderDetailsVo) {
        if (clyjGoodsOrderDetailsVo == null || clyjGoodsOrderDetailsVo.getClyjGoodsOrderDetails().size() < 1) {
            logger.error("资产变动------请求参数非法:" + "clyjGoodsOrderDetailsVo" + "---------" + clyjGoodsOrderDetailsVo);
            return ApiResult.fail("参数非法改");
        }
        List<ClyjGoodsOrderDetails> clyjGoodsOrderDetails = clyjGoodsOrderDetailsVo.getClyjGoodsOrderDetails();
        for (ClyjGoodsOrderDetails clyjGoodsOrderDetails2 : clyjGoodsOrderDetails) {
            QueryWrapper<ClyjUserPropertyRecord> QueryWrapper = new QueryWrapper<ClyjUserPropertyRecord>();
            QueryWrapper.eq("user_id", clyjGoodsOrderDetails2.getUserId());
            QueryWrapper.eq("trade_no", clyjGoodsOrderDetails2.getTradeNo());
            ClyjUserPropertyRecord clyjUserPropertyRecord = clyjUserPropertyRecordMapper.selectOne(QueryWrapper);
            if (clyjUserPropertyRecord == null) {
                return ApiResult.fail("无此订单");
            }
            /**状态，（0，已到账 1未到账状态）**/
            if (clyjUserPropertyRecord.getState().equals(BaseConstants.USABLE_STATE)) {
                return ApiResult.fail("重复更改");
            }
            /**
             * 开支（0收入 1支出）
             */
            String expenses = clyjUserPropertyRecord.getExpenses();
            /**
             * 资产类型(0 零钱 1红包 2积分,3余额)
             */
            String assetType = clyjUserPropertyRecord.getAssetType();
            ClyjUserProperty entity = clyjUserPropertyMapper.selectById(clyjGoodsOrderDetails2.getUserId());
            if (entity == null) {
                return ApiResult.fail("查询无信息");
            }
            if (expenses.equals(BaseConstants.INCOME_CODE)) {
                if (assetType.equals(BaseConstants.SHOESTRING_MONEY)) {
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayBalance();
                    /**冻结零钱(收入)**/
                    BigDecimal freezeShoestringMoney = entity.getFreezeShoestringMoney();
                    /** 零钱**/
                    BigDecimal shoestringMoney = entity.getShoestringMoney();
                    /**冻结零钱(收入)freezeShoestringMoney    大于或等于      支付零钱（vaule）**/
                    if (freezeShoestringMoney.compareTo(vaule) == 1 || freezeShoestringMoney.compareTo(vaule) == 0) {
                        entity.setFreezeShoestringMoney(freezeShoestringMoney.subtract(vaule));
                        entity.setShoestringMoney(shoestringMoney.add(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结零钱(收入):" + freezeShoestringMoney + "---------" + "PayBalance" + vaule);
                        ApiResult.fail("解冻零钱大于冻结零钱，参数非法");
                    }
                } else if (assetType.equals(BaseConstants.RED_PACKET)) {
                    /** 支付红包**/
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayRedPacket();
                    /**冻结红包(收入)**/
                    BigDecimal freezeRedPacket = entity.getFreezeRedPacket();
                    /** 红包**/
                    BigDecimal redPacket = entity.getRedPacket();
                    /**冻结红包(收入)freezeShoestringMoney    大于或等于      支付红包（vaule）**/
                    if (freezeRedPacket.compareTo(vaule) == 1 || freezeRedPacket.compareTo(vaule) == 0) {
                        entity.setFreezeRedPacket(freezeRedPacket.subtract(vaule));
                        entity.setRedPacket(redPacket.add(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结红包(收入):" + freezeRedPacket + "---------" + "payRedPacket" + vaule);
                        ApiResult.fail("解冻红包大于冻结红包，参数非法");
                    }
                } else if (assetType.equals(BaseConstants.INTEGRAL)) {
                    /**支付积分**/
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayIntegral();
                    /**冻结积分(收入))**/
                    BigDecimal freezeIntegral = entity.getFreezeIntegral();
                    /**积分**/
                    BigDecimal integral = entity.getIntegral();
                    /**冻结积分(收入)freezeShoestringMoney    大于或等于      支付积分（vaule）**/
                    if (freezeIntegral.compareTo(vaule) == 1 || freezeIntegral.compareTo(vaule) == 0) {
                        entity.setFreezeIntegral(freezeIntegral.subtract(vaule));
                        entity.setIntegral(integral.add(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结红包(收入):" + freezeIntegral + "---------" + "PayIntegral" + vaule);
                        ApiResult.fail("解冻积分大于冻结积分，参数非法");
                    }
                } else if (assetType.equals(BaseConstants.ACCOUNT)) {
                    /**实际支付金额**/
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayAmount();
                    /** 冻结余额(收入)**/
                    BigDecimal freezeAccount = entity.getFreezeIntegral();
                    /**余额**/
                    BigDecimal account = entity.getAccount();
                    /**冻结余额(收入)freezeShoestringMoney    大于或等于     实际支付金额（vaule）**/
                    if (freezeAccount.compareTo(vaule) == 1 || freezeAccount.compareTo(vaule) == 0) {
                        entity.setFreezeAccount(freezeAccount.subtract(vaule));
                        entity.setAccount(account.add(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结余额(收入):" + freezeAccount + "---------" + "PayAmount" + vaule);
                        ApiResult.fail("解冻余额大于冻结余额，参数非法");
                    }
                }
            } else if (expenses.equals(BaseConstants.PAY_CODE)) {
                if (assetType.equals(BaseConstants.SHOESTRING_MONEY)) {
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayBalance();
                    /**冻结零钱(支出)**/
                    BigDecimal freezePayShoestringMoney = entity.getFreezePayShoestringMoney();
                    /**冻结零钱(收入)freezeShoestringMoney    大于或等于      支付零钱（vaule）**/
                    if (freezePayShoestringMoney.compareTo(vaule) == 1 || freezePayShoestringMoney.compareTo(vaule) == 0) {
                        entity.setFreezeShoestringMoney(freezePayShoestringMoney.subtract(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结零钱(支出):" + freezePayShoestringMoney + "---------" + "PayBalance" + vaule);
                        ApiResult.fail("解冻零钱大于冻结零钱，参数非法");
                    }
                } else if (assetType.equals(BaseConstants.RED_PACKET)) {
                    /** 支付红包**/
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayRedPacket();
                    /** 冻结红包（支出）)**/
                    BigDecimal freezePayRedPacket = entity.getFreezePayRedPacket();
                    /**冻结红包(支出)freezePayRedPacket    大于或等于      支付红包（vaule）**/
                    if (freezePayRedPacket.compareTo(vaule) == 1 || freezePayRedPacket.compareTo(vaule) == 0) {
                        entity.setFreezePayRedPacket(freezePayRedPacket.subtract(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结红包(支出):" + freezePayRedPacket + "---------" + "payRedPacket" + vaule);
                        ApiResult.fail("解冻红包大于冻结红包，参数非法");
                    }
                } else if (assetType.equals(BaseConstants.INTEGRAL)) {
                    /**支付积分**/
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayIntegral();
                    /**冻结积分（支出）**/
                    BigDecimal freezePayIntegral = entity.getFreezePayIntegral();
                    /**冻结积分（支出）)freezePayIntegral    大于或等于      支付积分（vaule）**/
                    if (freezePayIntegral.compareTo(vaule) == 1 || freezePayIntegral.compareTo(vaule) == 0) {
                        entity.setFreezeIntegral(freezePayIntegral.subtract(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结红包(支出):" + freezePayIntegral + "---------" + "PayIntegral" + vaule);
                        ApiResult.fail("解冻积分大于冻结积分，参数非法");
                    }
                } else if (assetType.equals(BaseConstants.ACCOUNT)) {
                    /**实际支付金额**/
                    BigDecimal vaule = clyjGoodsOrderDetails2.getPayAmount();
                    /**  冻结余额(支出)**/
                    BigDecimal freezePayAccount = entity.getFreezePayAccount();
                    /** 冻结余额(支出)freezePayAccount    大于或等于     实际支付金额（vaule）**/
                    if (freezePayAccount.compareTo(vaule) == 1 || freezePayAccount.compareTo(vaule) == 0) {
                        entity.setFreezeAccount(freezePayAccount.subtract(vaule));
                        clyjUserPropertyRecordMapper.updateById(clyjUserPropertyRecord.setState(BaseConstants.USABLE_STATE));
                        clyjUserPropertyMapper.updateById(entity);
                    } else {
                        logger.error("资产变动------冻结余额(支出):" + freezePayAccount + "---------" + "PayAmount" + vaule);
                        ApiResult.fail("解冻余额大于冻结余额，参数非法");
                    }
                }
            }

        }
        return ApiResult.success();
    }
}
